package org.study.swing.study01.component;

import javax.swing.*;
import java.awt.*;

/**
 * 作用描述
 * 参考 https://blog.csdn.net/weixin_74141222/article/details/134828362
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月11日
 */
public class ComboBoxDemo1 {
    public static void main(String[] args) {
        // testComboBox1();
        testComboBox2();
        // new MyComboBoxDemo1();
        // new MyComboBoxDemo2();
        // new MyComboBoxDemo3();
    }

    public static void testComboBox1() {
        JFrame frame = new JFrame("示例：ComboBox");

        String[] items = {"选项1", "选项2", "选项3", "选项4"};
        JComboBox<String> comboBox = new JComboBox<>(items);
        frame.add(comboBox);
        // frame.add(comboBox, BorderLayout.CENTER);
        // comboBox.setSelectedIndex(1);
        comboBox.setSelectedItem("选项3");

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void testComboBox2() {
        JFrame frame = new JFrame("示例：ComboBox");

        JPanel panel = new JPanel();
        String[] items = {"选项1", "选项2", "选项3", "选项4"};
        JComboBox<String> comboBox = new JComboBox<>(items);
        comboBox.addItem("选项100");
        comboBox.setSelectedItem("选项3");
        comboBox.removeItemAt(1);
        panel.add(comboBox);
        frame.setContentPane(panel);

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    static class MyComboBoxDemo1 extends JFrame {
        public MyComboBoxDemo1() {
            super.setTitle("示例：ComboBox");

            Container container = super.getContentPane();
            // 创建下拉框对象comboBox
            JComboBox<String> comboBox = new JComboBox<>();
            // 添加选项内容
            comboBox.addItem("选项1");
            comboBox.addItem("选项2");
            comboBox.addItem("选项3");
            comboBox.addItem("选项4");
            comboBox.setSelectedItem("选项3");
            container.add(comboBox);

            super.setSize(600, 400);
            // 将窗口居中显示在屏幕上
            super.setLocationRelativeTo(null);
            super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            super.setVisible(true);
        }
    }

    static class MyComboBoxDemo2 extends JFrame {
        public MyComboBoxDemo2() {
            super.setTitle("示例：ComboBox");

            Container container = super.getContentPane();
            // 创建选项内容
            String[] items = {"选项1", "选项2", "选项3", "选项4"};
            // 创建下拉框对象comboBox
            JComboBox<String> comboBox = new JComboBox<>(items);
            comboBox.setSelectedItem("选项3");
            container.add(comboBox);

            super.setSize(600, 400);
            // 将窗口居中显示在屏幕上
            super.setLocationRelativeTo(null);
            super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            super.setVisible(true);
        }
    }

    static class MyComboBoxDemo3 extends JFrame {
        public MyComboBoxDemo3() {
            super.setTitle("示例：ComboBox");

            Container container = super.getContentPane();
            // 创建String数组对象
            String[] items = {"选项1", "选项2", "选项3", "选项4"};
            // 将下拉框中的选项封装为ComboBoxModel
            ComboBoxModel<String> comboBoxModel = new DefaultComboBoxModel<>(items);
            // 创建下拉框对象comboBox
            JComboBox<String> comboBox = new JComboBox<>();
            comboBox.setModel(comboBoxModel);
            comboBox.setSelectedItem("选项3");
            container.add(comboBox);

            super.setSize(600, 400);
            // 将窗口居中显示在屏幕上
            super.setLocationRelativeTo(null);
            super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            super.setVisible(true);
        }
    }
}
